<?php
/**
 * Created by PhpStorm.
 * User: luohaixiong
 * Date: 17/3/9
 * Time: 下午5:34
 */

$name = "";
$err = "";

if($_SERVER["REQUEST_METHOD"] == "POST"){
    if(isset($_FILES["file"])){
        $file = $_FILES["file"];
        if($file["error"] > 0){
            switch ($file["error"]){
                case 1 :
                    $err = "文件大小比php.ini中upload_max_filesize指定值要大";
                    break;
                case 2 :
                    $err = "文件的小比表单的MAX_FILE_SIZE指定的值大";
                    break;
                case 3 :
                    $err = "文件上传不完整（可能因为请求时间过长被终止）";
                    break;
                case 4 :
                    $err = "没有文件随着这个请求上传";
                    break;
                case 6 :
                    $err = "在php.ini中没有指定临时文件夹";
                    break;
                case 7 :
                    $err = "文件写入失败";
                    break;
                default :
                    $err = "未知错误";
            }
        }
//    else if(strpos($file["type"], "image") === false) {
//        $err = "只能上传图片";
//    }
//    else if($file["size"] > 1024 * 1024){
//        $err = "上传的图片必须小于1M";
//    }
        else{
            if(move_uploaded_file($file["tmp_name"], "/Applications/XAMPP/xamppfiles/htdocs/test/" . $file["name"]) !== false){
                $name = $file["name"] . "上传成功";
            }else{
                $err = "移动文件失败";
            }

        }
    }else{
        $err = "没收到上传的文件";
    }
}else{
    $err = "只支付POST请求";
}

if(empty($err)){
    echo $name;
}else{
    echo $err;
}